// 通用事件类型
export interface WebSocketConnectionEvent {
  clientId: string;
  ip: string;
  timestamp: string;
}

export interface WebSocketDisconnectionEvent extends WebSocketConnectionEvent {}
export interface WebSocketMessageEvent {
  clientId: string;
  message: string;
  timestamp: string;
}

// 进度事件类型
export interface ProgressEvent {
  taskId: string;
  progress: number;
}

export interface CompleteEvent {
  taskId: string;
  filePath: string;
}

export interface ErrorEvent {
  taskId: string;
  error: string;
}

export interface DownloadProgressEvent {
  bytesPerSecond?: number;
  percent: number;
  transferred: number;
  total: number;
}

export interface DownloadCompleteEvent {
  filePath: string;
}

export interface DownloadErrorEvent {
  error: string;
}

// 主通信接口
export interface IpcAPI {
  // 日志方法
  log: {
    info: (message: string, ...args: any[]) => void;
    warn: (message: string, ...args: any[]) => void;
    error: (message: string, ...args: any[]) => void;
    debug: (message: string, ...args: any[]) => void;
  };

  // 文件操作
  downloadFile: (payload: any) => void;
  saveBase64Image: (params: { base64Data: string; directory: string; fileName: string; imagesPath: string }) => Promise<any>;
  readMetadata: (filePath: string|File,map?:object) => Promise<any>;
  readFolderMeta: () => Promise<any>;
  openFileDialog: () => Promise<any>;
  selectFileFolder: () => Promise<any>;
  openFileFolder: (folderPath: string) => Promise<any>;

  // 目录扫描
  scanDirectories: (targetPath: string) => Promise<any>;
  scanImages: (basePath: string, folderName: string) => Promise<any>;

  // LoRA 相关
  scanLoras: (loraPath: string) => Promise<any>;
  getLoraConfig: (loraPath: string) => Promise<any>;
  saveLoraConfig: (loraPath: string, data: any) => Promise<any>;
  saveLoraImage: (imagePath: string|File, loraPath: string) => Promise<any>;
  saveWebpImage: (imagePath: string|File, targetPath: string) => Promise<any>;
  cacheImage: (cachePath:string,array:ArrayBuffer|File) => Promise<any>;
  getLoraMetadata: (loraPath: string) => Promise<any>;
  getCivitaiMetadata: (sha256:string|undefined,loraPath: string,replaceCover: boolean|undefined) => Promise<any>;

  // 存储操作
  localSet: (key: string, value: any) => Promise<any>;
  localGet: (key: string) => Promise<any>;
  localRemove: (key: string) => Promise<any>;
  localClear: () => Promise<any>;

  sessionSet: (key: string, value: any) => Promise<any>;
  sessionGet: (key: string) => Promise<any>;
  sessionRemove: (key: string) => Promise<any>;
  sessionClear: () => Promise<any>;

  // 配置管理
  getGlobalConfig: () => Promise<any>;
  saveConfig: (config: any) => Promise<any>;

  sendWebsocketData: (data:any,type:string) => Promise<any>;

  // WebSocket 功能
  onWebSocketConnection: (callback: (event: WebSocketConnectionEvent) => void) => () => void;
  onWebSocketDisconnection: (callback: (event: WebSocketDisconnectionEvent) => void) => () => void;
  onWebSocketMessage: (callback: (event: WebSocketMessageEvent) => void) => () => void;
  broadcastMessage: (message: string) => Promise<any>;
  sendToClient: (clientId: string, message: string,type: string) => Promise<any>;
  getSocketClients: () => Promise<any>;


  // CSV 导入进度
  onCsvImportProgress: (callback: (progress: any) => void) => void;

  onDownloadProgress: (callback: (progress: DownloadProgressEvent) => void) => () => void;
  onDownloadComplete: (callback: (result: DownloadCompleteEvent) => void) => () => void;
  onDownloadError: (callback: (error: DownloadErrorEvent) => void) => () => void;
  onImportProgress: (callback: (progress: any) => void) => () => void;
}

// 平台检测类型
export type Platform = 'electron' | 'tauri' | 'web';
